#!/usr/bin/env node

const inquirer = require('inquirer')
const path = require('path')
const fs = require('fs')
const ejs = require('ejs')

inquirer.prompt([
  {
    type: 'input',
    name: 'name',
    message: 'your projcet name?'
  }
]).then(anwsers => {
  // 根据用户输出生成文件
  const tmplDir = path.join(__dirname, 'templates')
  const destDir = process.cwd()
  console.log(destDir)

  fs.readdir(tmplDir, (err, files) => {
    if (err) throw err

    files.forEach(file => {
      // 模板渲染
      ejs.renderFile(path.join(tmplDir, file), anwsers, (err, result) => {
        if (err) throw err
        // 写入目标文件路径
        fs.writeFileSync(path.join(destDir, file), result)
      })
    })
  })
})